//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Microsoft.Research.ScientificWorkflow.ManagementStudioControls.Common;
using Microsoft.Research.ScientificWorkflow.ManagementStudioModels;

namespace Microsoft.Research.ScientificWorkflow.ManagementStudioControls.WorkflowManagerModule
{
    /// <summary>
    /// Interaction logic for RunningWorkflowsListView.xaml
    /// </summary>
    public sealed partial class RunningWorkflowsListView : UserControl
    {
        public RunningWorkflowsListView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// The event handler for the context menu opening event on the listview item.
        /// </summary>
        /// <param name="sender">The source.</param>
        /// <param name="e">The event arguments.</param>
        private void ContextMenu_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            IListPresenter listPresenter = this.DataContext as IListPresenter;
            ListViewItem selectedItem = sender as ListViewItem;

            ContextMenu contextMenu = selectedItem.ContextMenu;
            object jobModel = selectedItem.DataContext;

            foreach (MenuItem menuItem in listPresenter.MenuItemCollection)
            {
                System.Windows.Controls.ContextMenu parent = menuItem.Parent as System.Windows.Controls.ContextMenu;
                if (parent != null)
                {
                    parent.Items.Remove(menuItem);
                }

                menuItem.CommandParameter = jobModel;
                contextMenu.Items.Add(menuItem);
            }
        }

        /// <summary>
        /// Event handler for the deouble click event.
        /// </summary>
        /// <param name="sender">The source.</param>
        /// <param name="e">The event arguments.</param>
        private void ListViewItemDoubleClicked(object sender, RoutedEventArgs e)
        {
            ListViewItem item = sender as ListViewItem;
            this.DisplayListView.SelectedItem = item.DataContext;

            // Invoke the trident composer on double click.
            (this.DataContext as RunningWorkflowsListPresenter).ShowMonitorCommand.Execute(item.DataContext as JobViewModel);
        }
    }


    /// <summary>
    /// A converter which is used by bindings.Returns an empty string if the date is invalid.
    /// </summary>
    public class DateTimeConverter : IValueConverter
    {
        /// <summary>
        /// Convert based on value and parameter.
        /// </summary>
        /// <param name="value">
        /// The current value.
        /// </param>
        /// <param name="targetType">
        /// The expected type.
        /// </param>
        /// <param name="parameter">
        /// Passed parameter.
        /// </param>
        /// <param name="culture">
        /// Culture.
        /// </param>
        /// <returns>
        /// Modified value.
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            DateTime dateTime = (DateTime)value;

            if (dateTime.Equals(DateTime.MinValue))
            {
                return Microsoft.Research.ScientificWorkflow.TridentUtilities.ManagementStudioResourceManager.GetString("InvalidDateText");
            }

            return value;
        }

        /// <summary>
        /// Convert back based on value and parameter.
        /// </summary>
        /// <param name="value">
        /// The current value.
        /// </param>
        /// <param name="targetType">
        /// The expected type.
        /// </param>
        /// <param name="parameter">
        /// Passed parameter.
        /// </param>
        /// <param name="culture">
        /// Culture.
        /// </param>
        /// <returns>
        /// Old value.
        /// </returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }

    /// <summary>
    /// Returns a proper minwidth for the header.
    /// </summary>
    public class MinWidthConverter : IValueConverter
    {
        /// <summary>
        /// Convert based on value and parameter.
        /// </summary>
        /// <param name="value">
        /// The current value.
        /// </param>
        /// <param name="targetType">
        /// The expected type.
        /// </param>
        /// <param name="parameter">
        /// Passed parameter.
        /// </param>
        /// <param name="culture">
        /// Culture.
        /// </param>
        /// <returns>
        /// Modified value.
        /// </returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Double width = (Double)value;

            return width - 12;
        }

        /// <summary>
        /// Convert back based on value and parameter.
        /// </summary>
        /// <param name="value">
        /// The current value.
        /// </param>
        /// <param name="targetType">
        /// The expected type.
        /// </param>
        /// <param name="parameter">
        /// Passed parameter.
        /// </param>
        /// <param name="culture">
        /// Culture.
        /// </param>
        /// <returns>
        /// Old value.
        /// </returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }
}
